package com.pine.dbant.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.pine.dbant.core.entity.Relation;
import com.pine.dbant.core.mapper.RelationMapper;
import com.pine.dbant.core.model.RelationType;
import com.pine.dbant.core.service.RelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RelationServiceImpl implements RelationService {

    @Autowired
    private RelationMapper relationMapper;

    @Override
    public void deleteByResId(Long resId, RelationType type) {
        relationMapper.delete(new LambdaUpdateWrapper<Relation>().eq(Relation::getResId, resId)
                .eq(Relation::getType, type.name()));
    }

    @Override
    public void deleteByTargetId(Long targetId, RelationType type) {
        relationMapper.delete(new LambdaUpdateWrapper<Relation>().eq(Relation::getTargetId, targetId)
                .eq(Relation::getType, type.name()));
    }

    @Override
    public void delete(Long resId, Long targetId, RelationType type) {
        relationMapper.delete(new LambdaUpdateWrapper<Relation>().eq(Relation::getResId, resId)
                .eq(Relation::getTargetId, targetId)
                .eq(Relation::getType, type.name()));
    }

    @Override
    public Relation getByTargetId(Long targetId, RelationType type) {
        return relationMapper.selectOne(new LambdaUpdateWrapper<Relation>().eq(Relation::getTargetId, targetId)
                .eq(Relation::getType, type.name()));
    }

    @Override
    public void save(Relation relation) {
        relationMapper.insert(relation);
    }
}
